﻿-- Store Tách Tên và Họ của Tác giả
create procedure sp_Update_HoVaTen @MATACGIA varchar(10), @HOVATEN nvarchar(100)

as
	begin
		
		declare @iDoc int

		set @iDoc = len(@HOVATEN)

		update tb_Tacgia 
				set HOTACGIA = substring(@HOVATEN, 0, @iDoc - len(Reverse(substring(Reverse(@HOVATEN), 0, Patindex('% %',Reverse(@HOVATEN))))) )
					, TENTACGIA  = Reverse(substring(Reverse(@HOVATEN), 0, Patindex('% %',Reverse(@HOVATEN))) )
				where MATACGIA = @MATACGIA
	end


-- Sửa lại Store sp_Insert_Tacgia 
 
alter Procedure sp_Insert_Tacgia
	@MATACGIA varchar(10), @BUTDANH	nvarchar(50), @HOTACGIA	nvarchar(50	), @GIOITINH bit
As
	begin 

		Insert into tb_Tacgia(MATACGIA, BUTDANH, GIOITINH) 
			values(@MATACGIA, @BUTDANH, @GIOITINH)
		exec sp_Update_HoVaTen @MATACGIA, @HOTACGIA
	end

----
alter Procedure sp_Update_Tacgia

	@MATACGIA varchar(10), @BUTDANH	nvarchar(50), @HOTACGIA	nvarchar(50	), @GIOITINH bit

As
	begin
		Update tb_Tacgia Set MATACGIA=@MATACGIA, BUTDANH=@BUTDANH, GIOITINH=@GIOITINH Where MATACGIA=@MATACGIA
		exec sp_Update_HoVaTen @MATACGIA, @HOTACGIA
	end

----
alter Procedure [dbo].[sp_SelectAll_User]
As
Select a.Id, a.Ten, a.Matkhau, a.Hoten, a.VaitroID, b.Ten as TenVT, a.Images from tb_User a join tb_Vaitro b on a.VaitroID = b.Id

GO

--select * from tb_User
--alter table tb_User add Images nvarchar(1000) null

----

alter Procedure [dbo].[sp_Insert_User]
@Ten nvarchar(50), @Matkhau varchar(50), @Hoten nvarchar(50), @VaitroID bigint, @Images nvarchar(1000)
As
Insert into tb_User(Ten,Matkhau,Hoten,VaitroID,Images) values(@Ten,@Matkhau,@Hoten,@VaitroID,@Images)

GO

--- 
Create Procedure [dbo].[sp_SelectCobox_Dausach]
As
Select a.MADAUSACH, a.TENDAUSACH
From tb_Dausach a join tb_Theloai b on a.MATHELOAI=b.MATHELOAI join tb_Nhaxuatban c on a.MANHAXB=c.MANHAXB join tb_Cuatacgia d on a.MADAUSACH=d.MADAUSACH join tb_Tacgia e on d.MATACGIA=e.MATACGIA

GO


---

Create Procedure [dbo].[sp_SelectCobox_Kesach]
As
Select * From tb_Kesach

GO

